


#include "CxTransFacade.h"

using namespace ImitateTansfer;



CxTransFacade::CxTransFacade(size_t   portnumber):m_portnumber(portnumber),
	m_capturethread(portnumber,NULL),m_buffer(portnumber,NULL),m_wirtethread(portnumber,NULL),
    m_portgroup(portnumber,NULL),m_isBuilded(false)
{
}


CxTransFacade::~CxTransFacade(void)
{
    DestroyPart();
	cout<<"TransFacade have been destroy....."<<endl;
}


inline void        CxTransFacade::currentFrame(size_t number ,double value)
{
    sig_currentframe(number,value);
}


bool		CxTransFacade::ChangeCapture(LPCSTR filename , size_t  number)
{
	CX_ASSERT(filename);
	if(m_capturethread[number]==NULL)
	{
		m_capturethread[number]=new CxGrabThread(NULL,m_buffer[number],number);
        m_capturethread[number]->sig_framecount.connect(boost::bind(&CxTransFacade::currentFrame,this,number,_1));
    }

	return m_capturethread[number]->ChangeCapture(filename);
}



//建立整体
void		CxTransFacade::BuildPart(size_t  portnumber)
{

	CX_ASSERT(portnumber);

	std::string  hostname=boost::asio::ip::host_name();
	hostent * host=gethostbyname(hostname.c_str());
	char *name=inet_ntoa((*(in_addr*)host->h_addr_list[0]));

	m_service=new CxIPService(name,&m_portgroup,6655,portnumber);

	m_service->sig_connected.connect(boost::bind<void>(&CxTransFacade::SLOT_connectedclient,this,_1));


    for(BOOST_AUTO(i,m_buffer.begin());i!=m_buffer.end();++i)
	{
		*i=new CxWriteBuffer(8);
	}
	int j=0;
    for(BOOST_AUTO(i,m_wirtethread.begin());i!=m_wirtethread.end();++i,++j)
	{
		*i=new CxWriteThread(m_portgroup[j],m_buffer[j]);
	}

    m_isBuilded=true;
}



//解构整体
void        CxTransFacade::DestroyPart()
{

    for(BOOST_AUTO(i,m_capturethread.begin());i!=m_capturethread.end();++i)
    {
        if(*i!=NULL)
        {
            delete *i;
            *i=NULL;
        }
    }

    cout<<"CxTransFacade::GrabThread have been Destroy......"<<endl;
	
    for(BOOST_AUTO(i,m_wirtethread.begin());i!=m_wirtethread.end();++i)
	{
        if(*i != NULL)
        {
            delete *i;
            *i=NULL;
        }
	}

    cout<<"CxTransFacade::WriteThread have been Destroy......"<<endl;

    for(BOOST_AUTO(i,m_portgroup.begin());i!=m_portgroup.end();++i)
	{
        if(*i != NULL)
        {
            delete *i;
            *i=NULL;
        }
	}

    cout<<"CxTransFacade::Portgroup have been Destroy......"<<endl;


    m_service->s_ioservice.stop();
    m_service->s_pwatchthread->join();

    cout<<"io_service have been stoped....."<<endl;

    delete m_service->s_pwatchthread;
    m_service->s_pwatchthread=NULL;

    cout<<"m_service Watch thread have destroy....."<<endl;

    if(m_service!=NULL)
    {
        delete m_service;
        m_service = NULL;
    }

    m_isBuilded=false;
}


void		CxTransFacade::SLOT_portclose(size_t number)
{
	delete m_portgroup[number];
	m_portgroup[number]=NULL;
    sig_portclose(number);
}


void		CxTransFacade::SLOT_connectedclient(size_t number)
{
	m_portgroup[number]->sig_close.connect(boost::bind(&CxTransFacade::SLOT_portclose,this,number));
    sig_connectclient(number);
}


void            CxTransFacade::SLOT_setframecount(double value,size_t number)
{
    if(m_capturethread[number]!=NULL)
        m_capturethread[number]->SetFrame(value);
}

bool                CxTransFacade::SLOT_openfile(LPCSTR filename ,size_t number)
{
   return  ChangeCapture(filename,number);
}
